/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/** @defgroup uio Uio
 * @ingroup libc
 */

#ifndef _SYS_UIO_H_
#define _SYS_UIO_H_

#include <sys/cdefs.h>
#include <sys/types.h>
#ifndef __LITEOS__
#include <linux/uio.h>
#else
#include <liteos/uio.h>
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */


#ifndef __LITEOS__
int readv(int, const struct iovec*, int);
int writev(int, const struct iovec*, int);
#else

/**
* @ingroup uio
*
* @par Description:
* The readv() function shall be equivalent to read(), except as described below.
* The readv() function shall place the input data into the iovcnt buffers specified by the members
* of the iov array: iov[0], iov[1], ..., iov[iovcnt-1].
* Each iovec entry specifies the base address and length of an area in memory where data should be placed.
* The readv() function shall always fill an area completely before proceeding to the next.
*
* @param fd      [IN]     File descriptor (or socket descriptor) to read from
* @param iov     [IN/OUT] The array that user-provided to save the data
* @param iovcnt  [IN]     The maximum value of the array
*
* @attention
* <ul>
* <li>The iovcnt argument has no upper limit until out of memory.</li>
* </ul>
*
* @retval #ssize_t  A positive non-zero number of bytes read on suceess.
* @retval #0        End-of-file or failed in read().
* @retval #-1       The iov is a null pointer or out of memory.
*
* @par Errors
* <ul>
* <li><b>EBADF</b>: __fd is not a valid open file descriptor, or the file system doesn't support read().</li>
* <li><b>EFAULT</b>: __buf is outside your accessible address space.</li>
* <li><b>EACCES</b>: The file is a write-only file.</li>
* <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
* <li><b>ENOMEM</b>: Out of memory.</li>
* <li><b>EINVAL</b>: __count is greater than {YAFFS_MAX_FILE_SIZE} in yaffs filesystem.</li>
* <li><b>ENODEV</b>: The mount is not healthy.</li>
* </ul>
*
* @par Dependency:
* <ul><li>uio.h</li></ul>
* @see  read | writev
* @since Huawei LiteOS V100R001C00
*/
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);

/**
* @ingroup uio
*
* @par Description:
* The writev() function shall be equivalent to write(), except as described below.
* The writev() function shall gather output data from the iovcnt buffers specified by the members
* of the iov array: iov[0], iov[1], ..., iov[iovcnt-1].
* Each iovec entry specifies the base address and length of an area in memory from which data should be written.
* The writev() function shall always write a complete area before proceeding to the next.
*
* @param fd      [IN] File descriptor (or socket descriptor) to read from
* @param iov     [IN] The array of data that user-provided to write
* @param iovcnt  [IN] The maximum value of the array

*
* @attention
* <ul>
* <li>If all of the iov_len members in the array pointed to by iov are 0, it shall return -1.</li>
* <li>The iovcnt argument has no upper limit until out of memory.</li>
* </ul>
*
* @retval #ssize_t  On success, the number of bytes written are returned (zero indicates nothing was written).
* @retval #-1       On error, -1 is returned, and errno is set appropriately.
*
* @par Errors
* <ul>
* <li><b>EBADF</b>: __fd is not a valid open file descriptor, or the file system doesn't support write().</li>
* <li><b>EFAULT</b>: __buf is outside your accessible address space.</li>
* <li><b>EACCES/EROFS</b>: The file is a read-only file or the file system is read-only.</li>
* <li><b>EIO</b>: A physical I/O error has occurred.</li>
* <li><b>ENOMEM</b>: Out of memory.</li>
* <li><b>EINTR</b>: The write operation was terminated due to the receipt of a signal, no data was transferred.</li>
* <li><b>ENOSPC</b>: There was no free space remaining on the device containing the file.</li>
* <li><b>EAGAIN</b>: Non-blocking I/O has been selected using O_NONBLOCK and the write would block.</li>
* <li><b>EFBIG</b>: An attempt was made to write a file that exceeds the implementation defined maximum file
* size or the process' file size limit, or to write at a position past the maximum allowed offset.</li>
* <li><b>EINVAL</b>: The current position of file is less than zero.</li>
* <li><b>ENODEV</b>: The mount is not healthy.</li>
* </ul>
*
* @par Dependency:
* <ul><li>uio.h</li></ul>
* @see  readv | write
* @since Huawei LiteOS V100R001C00
*/
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
#endif

#if defined(__USE_GNU)
#if defined(__USE_FILE_OFFSET64)
ssize_t preadv(int, const struct iovec*, int, off_t) __RENAME(preadv64);
ssize_t pwritev(int, const struct iovec*, int, off_t) __RENAME(pwritev64);
#else
ssize_t preadv(int, const struct iovec*, int, off_t);
ssize_t pwritev(int, const struct iovec*, int, off_t);
#endif
ssize_t preadv64(int, const struct iovec*, int, off64_t);
ssize_t pwritev64(int, const struct iovec*, int, off64_t);
#endif

#if defined(__USE_GNU)
ssize_t process_vm_readv(pid_t, const struct iovec*, unsigned long, const struct iovec*, unsigned long, unsigned long);
ssize_t process_vm_writev(pid_t, const struct iovec*, unsigned long, const struct iovec*, unsigned long, unsigned long);
#endif


#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _SYS_UIO_H_ */
